package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.RoleVo;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> findByPage(
            @PathVariable(name = "pageNum")Integer pageNum,
            @PathVariable(name = "pageSize")Integer pageSize,
            @RequestBody RoleDto dto
    ){
        PageResponse<RoleVo> page = roleService.findByPage(pageNum,pageSize,dto.getRoleName());
        return ResponseResult.success(page);
    }
    @PutMapping
    public ResponseResult saveRole(@RequestBody RoleDto dto){
        Role role = BeanUtil.toBean(dto, Role.class);
        roleService.saveRole(role);
        return ResponseResult.success();
    }

    @PatchMapping
    public ResponseResult updateRole(@RequestBody RoleDto dto){
        roleService.update(dto);
        return ResponseResult.success();
    }

    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> findByRoleId(@PathVariable(name = "roleId")Long roleId){
        Set<String> resources = roleService.findByRoleId(roleId);
        return ResponseResult.success(resources);
    }

    @DeleteMapping("/{roleIds}")
    public ResponseResult deleteByRoleId(@PathVariable(name = "roleIds")Long roleId){
       roleService.deleteByRoleId(roleId);
       return ResponseResult.success();
    }
}
